package com.kyleswebspace.propertyanalyzer.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.FocusEvent;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import com.kyleswebspace.parser.FieldParser;
import com.kyleswebspace.propertyanalyzer.config.ArcaFont;
import com.kyleswebspace.propertyanalyzer.config.ColorBox;
import com.kyleswebspace.propertyanalyzer.model.DataModel;

public class CHMCTablesFrame extends JFrame implements Updateable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8515412499050430086L;
	
	private DataModel dm;
	
	//panel components
	private JPanel topPanel, bottomPanel, outterPanel;
	private JLabel topPanelRowALabel, topPanelRowBLabel, topPanelRowCLabel,
		topPanelRowDLabel, topPanelRowELabel, topPanelRowFLabel, topPanelRowGLabel;
	private JTextField topPanelRowAField, topPanelRowBField, topPanelRowCField,
		topPanelRowDField, topPanelRowEField, topPanelRowFField, topPanelRowGField;
	private JLabel topPanelAdditional;
	private JTextField topPanelAdditionalField;
	private JLabel bottomPanelRowALabel, bottomPanelRowBLabel, bottomPanelRowCLabel,
		bottomPanelRowDLabel, bottomPanelRowELabel, bottomPanelRowFLabel;
	private JTextField bottomPanelRowAField, bottomPanelRowBField, bottomPanelRowCField,
		bottomPanelRowDField, bottomPanelRowEField, bottomPanelRowFField;
	private JLabel bottomPanelAdditionalA, bottomPanelAdditionalB, bottomPanelAdditionalC, bottomPanelAdditionalD;
	private JTextField bottomPanelAdditionalFieldA, bottomPanelAdditionalFieldB, bottomPanelAdditionalFieldC, bottomPanelAdditionalFieldD;
	
	private final Font appFont = ArcaFont.appFont;
	private final Color innerPanelColor = ColorBox.innerPanelColor;
	private final Color backgroundPanelColor = ColorBox.backgroundPanelColor;
	
	
	public CHMCTablesFrame(DataModel dm) {
		this.dm = dm;
		dm.register(this);
		
		topPanel = new JPanel();
		topPanel.setBackground(innerPanelColor);
		bottomPanel = new JPanel();
		bottomPanel.setBackground(innerPanelColor);
		outterPanel = new JPanel();
		outterPanel.setBackground(backgroundPanelColor);
		
		outterPanel.setLayout(new GridBagLayout());
		
		topPanel.setLayout(new GridBagLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder("CMHC Table (1-4 Units)");
		topPanel.setBorder(title);
		topPanel.setBackground(innerPanelColor);
		bottomPanel.setLayout(new GridBagLayout());
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.ipadx = 20;
		
		//init components
		topPanelRowALabel = new JLabel("95%     to    100%     =");
		topPanelRowALabel.setFont(appFont);
		topPanelRowBLabel = new JLabel("90%     to     95%      =");
		topPanelRowBLabel.setFont(appFont);
		topPanelRowCLabel = new JLabel("85%     to     90%      =");
		topPanelRowCLabel.setFont(appFont);
		topPanelRowDLabel = new JLabel("80%     to     85%      =");
		topPanelRowDLabel.setFont(appFont);
		topPanelRowELabel = new JLabel("75%     to     80%      =");
		topPanelRowELabel.setFont(appFont);
		topPanelRowFLabel = new JLabel("65%     to     75%      =");
		topPanelRowFLabel.setFont(appFont);
		topPanelRowGLabel = new JLabel("  0%     to     65%       =");
		topPanelRowGLabel.setFont(appFont);
		topPanelAdditional = new JLabel("<html>For each additional<br/>year period beyond <br/>25" + 
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + 
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=</html>");
		topPanelAdditional.setFont(appFont);
		
		topPanelRowAField = new JTextField(2);
		topPanelRowAField.addFocusListener(this);
		topPanelRowAField.setFocusable(false);
		topPanelRowAField.setFont(appFont);
		topPanelRowAField.setHorizontalAlignment(JTextField.RIGHT);
		topPanelRowBField = new JTextField(2);
		topPanelRowBField.addFocusListener(this);
		topPanelRowBField.setFocusable(false);
		topPanelRowBField.setFont(appFont);
		topPanelRowBField.setHorizontalAlignment(JTextField.RIGHT);
		topPanelRowCField = new JTextField(2);
		topPanelRowCField.addFocusListener(this);
		topPanelRowCField.setFocusable(false);
		topPanelRowCField.setFont(appFont);
		topPanelRowCField.setHorizontalAlignment(JTextField.RIGHT);
		topPanelRowDField = new JTextField(2);
		topPanelRowDField.addFocusListener(this);
		topPanelRowDField.setFocusable(false);
		topPanelRowDField.setFont(appFont);
		topPanelRowDField.setHorizontalAlignment(JTextField.RIGHT);
		topPanelRowEField = new JTextField(2);
		topPanelRowEField.addFocusListener(this);
		topPanelRowEField.setFocusable(false);
		topPanelRowEField.setFont(appFont);
		topPanelRowEField.setHorizontalAlignment(JTextField.RIGHT);
		topPanelRowFField = new JTextField(2);
		topPanelRowFField.addFocusListener(this);
		topPanelRowFField.setFocusable(false);
		topPanelRowFField.setFont(appFont);
		topPanelRowFField.setHorizontalAlignment(JTextField.RIGHT);
		topPanelRowGField = new JTextField(2);
		topPanelRowGField.addFocusListener(this);
		topPanelRowGField.setFocusable(false);
		topPanelRowGField.setFont(appFont);
		topPanelRowGField.setHorizontalAlignment(JTextField.RIGHT);
		topPanelAdditionalField = new JTextField(2);
		topPanelAdditionalField.addFocusListener(this);
		topPanelAdditionalField.setFocusable(false);
		topPanelAdditionalField.setFont(appFont);
		topPanelAdditionalField.setHorizontalAlignment(JTextField.RIGHT);
		
		//top panel
		topPanel.add(topPanelRowALabel, constraints);
		constraints.gridx = 1;
		topPanel.add(topPanelRowAField, constraints);
		constraints.gridy = 1;
		constraints.gridx = 0;
		topPanel.add(topPanelRowBLabel, constraints);
		constraints.gridx = 1;
		topPanel.add(topPanelRowBField, constraints);
		constraints.gridy = 2;
		constraints.gridx = 0;
		topPanel.add(topPanelRowCLabel, constraints);
		constraints.gridx = 1;
		topPanel.add(topPanelRowCField, constraints);
		constraints.gridy = 3;
		constraints.gridx = 0;
		topPanel.add(topPanelRowDLabel, constraints);
		constraints.gridx = 1;
		topPanel.add(topPanelRowDField, constraints);
		constraints.gridy = 4;
		constraints.gridx = 0;
		topPanel.add(topPanelRowDLabel, constraints);
		constraints.gridx = 1;
		topPanel.add(topPanelRowDField, constraints);
		constraints.gridy = 5;
		constraints.gridx = 0;
		topPanel.add(topPanelRowELabel, constraints);
		constraints.gridx = 1;
		topPanel.add(topPanelRowEField, constraints);
		constraints.gridy = 6;
		constraints.gridx = 0;
		topPanel.add(topPanelRowFLabel, constraints);
		constraints.gridx = 1;
		topPanel.add(topPanelRowFField, constraints);
		constraints.gridy = 7;
		constraints.gridx = 0;
		topPanel.add(topPanelRowGLabel, constraints);
		constraints.gridx = 1;
		topPanel.add(topPanelRowGField, constraints);
		constraints.gridx = 0;
		constraints.gridy = 8;
		//separator
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridwidth = 2;
		topPanel.add(new JSeparator(SwingConstants.HORIZONTAL),constraints);
		constraints.fill = GridBagConstraints.NONE;
		constraints.gridwidth = 1;
		constraints.gridy = 9;
		topPanel.add(topPanelAdditional, constraints);
		constraints.gridx = 1;
		constraints.anchor = GridBagConstraints.SOUTH;
		topPanel.add(topPanelAdditionalField, constraints);
		constraints.anchor = GridBagConstraints.NONE;
		
		//bottom panel
		bottomPanelRowALabel = new JLabel("85%     to    100%     =");
		bottomPanelRowALabel.setFont(appFont);
		bottomPanelRowBLabel = new JLabel("80%     to     85%      =");
		bottomPanelRowBLabel.setFont(appFont);
		bottomPanelRowCLabel = new JLabel("75%     to     80%      =");
		bottomPanelRowCLabel.setFont(appFont);
		bottomPanelRowDLabel = new JLabel("70%     to     75%      =");
		bottomPanelRowDLabel.setFont(appFont);
		bottomPanelRowELabel = new JLabel("65%     to     70%      =");
		bottomPanelRowELabel.setFont(appFont);
		bottomPanelRowFLabel = new JLabel("  0%     to     65%       =");
		bottomPanelRowFLabel.setFont(appFont);
		bottomPanelAdditionalA = new JLabel("<html>For each additional<br/>year period beyond <br/>25" + 
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + 
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=</html>");
		bottomPanelAdditionalA.setFont(appFont);
		bottomPanelAdditionalB = new JLabel("<html>Application fee for<br/>each unit up to 100&nbsp;&nbsp;&nbsp;&nbsp;=</html>");
		bottomPanelAdditionalB.setFont(appFont);
		bottomPanelAdditionalC = new JLabel("<html>Application fee for<br/>each unit exceeding&nbsp;&nbsp;&nbsp;=</html>");
		bottomPanelAdditionalC.setFont(appFont);
		bottomPanelAdditionalD = new JLabel("<html>Maximum application<br/>fee per loan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + 
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =</html>");
		bottomPanelAdditionalD.setFont(appFont);
		
		bottomPanelRowAField = new JTextField(2);
		bottomPanelRowAField.addFocusListener(this);
		bottomPanelRowAField.setFocusable(false);
		bottomPanelRowAField.setFont(appFont);
		bottomPanelRowAField.setHorizontalAlignment(JTextField.RIGHT);
		bottomPanelRowBField = new JTextField(2);
		bottomPanelRowBField.addFocusListener(this);
		bottomPanelRowBField.setFocusable(false);
		bottomPanelRowBField.setFont(appFont);
		bottomPanelRowBField.setHorizontalAlignment(JTextField.RIGHT);
		bottomPanelRowCField = new JTextField(2);
		bottomPanelRowCField.addFocusListener(this);
		bottomPanelRowCField.setFocusable(false);
		bottomPanelRowCField.setFont(appFont);
		bottomPanelRowCField.setHorizontalAlignment(JTextField.RIGHT);
		bottomPanelRowDField = new JTextField(2);
		bottomPanelRowDField.addFocusListener(this);
		bottomPanelRowDField.setFocusable(false);
		bottomPanelRowDField.setFont(appFont);
		bottomPanelRowDField.setHorizontalAlignment(JTextField.RIGHT);
		bottomPanelRowEField = new JTextField(2);
		bottomPanelRowEField.addFocusListener(this);
		bottomPanelRowEField.setFocusable(false);
		bottomPanelRowEField.setFont(appFont);
		bottomPanelRowEField.setHorizontalAlignment(JTextField.RIGHT);
		bottomPanelRowFField = new JTextField(2);
		bottomPanelRowFField.addFocusListener(this);
		bottomPanelRowFField.setFocusable(false);
		bottomPanelRowFField.setFont(appFont);
		bottomPanelRowFField.setHorizontalAlignment(JTextField.RIGHT);
		bottomPanelAdditionalFieldA = new JTextField(2);
		bottomPanelAdditionalFieldA.addFocusListener(this);
		bottomPanelAdditionalFieldA.setFocusable(false);
		bottomPanelAdditionalFieldA.setFont(appFont);
		bottomPanelAdditionalFieldA.setHorizontalAlignment(JTextField.RIGHT);
		bottomPanelAdditionalFieldB = new JTextField(2);
		bottomPanelAdditionalFieldB.addFocusListener(this);
		bottomPanelAdditionalFieldB.setFocusable(false);
		bottomPanelAdditionalFieldB.setFont(appFont);
		bottomPanelAdditionalFieldB.setHorizontalAlignment(JTextField.RIGHT);
		bottomPanelAdditionalFieldC = new JTextField(2);
		bottomPanelAdditionalFieldC.addFocusListener(this);
		bottomPanelAdditionalFieldC.setFocusable(false);
		bottomPanelAdditionalFieldC.setFont(appFont);
		bottomPanelAdditionalFieldC.setHorizontalAlignment(JTextField.RIGHT);
		bottomPanelAdditionalFieldD = new JTextField(2);
		bottomPanelAdditionalFieldD.addFocusListener(this);
		bottomPanelAdditionalFieldD.setFocusable(false);
		bottomPanelAdditionalFieldD.setFont(appFont);
		bottomPanelAdditionalFieldD.setHorizontalAlignment(JTextField.RIGHT);
		
		bottomPanel.setLayout(new GridBagLayout());
		title = BorderFactory.createTitledBorder("CMHC Table (5+ Units)");
		bottomPanel.setBorder(title);
		bottomPanel.setBackground(innerPanelColor);
		bottomPanel.setLayout(new GridBagLayout());
		constraints = new GridBagConstraints();
		constraints.ipadx = 20;
		
		bottomPanel.add(bottomPanelRowALabel, constraints);
		constraints.gridx = 1;
		bottomPanel.add(bottomPanelRowAField, constraints);
		constraints.gridy = 1;
		constraints.gridx = 0;
		bottomPanel.add(bottomPanelRowBLabel, constraints);
		constraints.gridx = 1;
		bottomPanel.add(bottomPanelRowBField, constraints);
		constraints.gridy = 2;
		constraints.gridx = 0;
		bottomPanel.add(bottomPanelRowCLabel, constraints);
		constraints.gridx = 1;
		bottomPanel.add(bottomPanelRowCField, constraints);
		constraints.gridy = 3;
		constraints.gridx = 0;
		bottomPanel.add(bottomPanelRowDLabel, constraints);
		constraints.gridx = 1;
		bottomPanel.add(bottomPanelRowDField, constraints);
		constraints.gridy = 4;
		constraints.gridx = 0;
		bottomPanel.add(bottomPanelRowDLabel, constraints);
		constraints.gridx = 1;
		bottomPanel.add(bottomPanelRowDField, constraints);
		constraints.gridy = 5;
		constraints.gridx = 0;
		bottomPanel.add(bottomPanelRowELabel, constraints);
		constraints.gridx = 1;
		bottomPanel.add(bottomPanelRowEField, constraints);
		constraints.gridy = 6;
		constraints.gridx = 0;
		bottomPanel.add(bottomPanelRowFLabel, constraints);
		constraints.gridx = 1;
		bottomPanel.add(bottomPanelRowFField, constraints);
		//constraints.gridy = 7;
		//constraints.gridx = 0;
		//bottomPanel.add(bottomPanelRowGLabel, constraints);
		//constraints.gridx = 1;
		//bottomPanel.add(bottomPanelRowGField, constraints);
		constraints.gridx = 0;
		constraints.gridy = 7;
		
		//separator
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridwidth = 2;
		bottomPanel.add(new JSeparator(SwingConstants.HORIZONTAL),constraints);
		constraints.fill = GridBagConstraints.NONE;
		constraints.gridwidth = 1;
		constraints.gridy = 8;
		bottomPanel.add(bottomPanelAdditionalA, constraints);
		constraints.gridx = 1;
		constraints.anchor = GridBagConstraints.SOUTH;
		bottomPanel.add(bottomPanelAdditionalFieldA, constraints);
		constraints.anchor = GridBagConstraints.EAST;
		constraints.gridx = 0;
		constraints.gridy = 9;
		//separator
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridwidth = 2;
		bottomPanel.add(new JSeparator(SwingConstants.HORIZONTAL),constraints);
		constraints.fill = GridBagConstraints.NONE;
		constraints.gridwidth = 1;
		constraints.gridy = 10;
		bottomPanel.add(bottomPanelAdditionalB, constraints);
		constraints.gridx = 1;
		constraints.anchor = GridBagConstraints.SOUTH;
		bottomPanel.add(bottomPanelAdditionalFieldB, constraints);
		constraints.anchor = GridBagConstraints.EAST;
		constraints.gridx = 0;
		constraints.gridy = 11;
		//separator
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridwidth = 2;
		bottomPanel.add(new JSeparator(SwingConstants.HORIZONTAL),constraints);
		constraints.fill = GridBagConstraints.NONE;
		constraints.gridwidth = 1;
		constraints.gridy = 12;
		bottomPanel.add(bottomPanelAdditionalC, constraints);
		constraints.gridx = 1;
		constraints.anchor = GridBagConstraints.SOUTH;
		bottomPanel.add(bottomPanelAdditionalFieldC, constraints);
		constraints.anchor = GridBagConstraints.EAST;
		constraints.gridx = 0;
		constraints.gridy = 13;
		//separator
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridwidth = 2;
		bottomPanel.add(new JSeparator(SwingConstants.HORIZONTAL),constraints);
		constraints.fill = GridBagConstraints.NONE;
		constraints.gridwidth = 1;
		constraints.gridy = 14;
		bottomPanel.add(bottomPanelAdditionalD, constraints);
		constraints.gridx = 1;
		constraints.anchor = GridBagConstraints.SOUTH;
		bottomPanel.add(bottomPanelAdditionalFieldD, constraints);
		
		
		

		
		//reset GridBagConstraints
		constraints = new GridBagConstraints();
		constraints.insets = new Insets(10, 10, 10, 10);
		outterPanel.add(topPanel, constraints);
		constraints.gridy = 1;
		outterPanel.add(bottomPanel, constraints);
		this.add(outterPanel);
		this.pack();
		this.setTitle("CMHC Tables");
		this.setLocationRelativeTo(null);
		
		updateFields();
	}


	public void updateFields() {
		this.topPanelRowAField.setText(dm.getCmhc1());
		this.topPanelRowBField.setText(dm.getCmhc2() + "%");
		this.topPanelRowCField.setText(dm.getCmhc3() + "%");
		this.topPanelRowDField.setText(dm.getCmhc4() + "%");
		this.topPanelRowEField.setText(dm.getCmhc5() + "%");
		this.topPanelRowFField.setText(dm.getCmhc6() + "%");
		this.topPanelRowGField.setText(dm.getCmhc7() + "%");
		this.topPanelAdditionalField.setText(dm.getCmhc8() + "%");
		
		
		this.bottomPanelRowAField.setText(dm.getCmhcB1());
		this.bottomPanelRowBField.setText(dm.getCmhcB2() + "%");
		this.bottomPanelRowCField.setText(dm.getCmhcB3() + "%");
		this.bottomPanelRowDField.setText(dm.getCmhcB4() + "%");
		this.bottomPanelRowEField.setText(dm.getCmhcB5() + "%");
		this.bottomPanelRowFField.setText(dm.getCmhcB6() + "%");
		this.bottomPanelAdditionalFieldA.setText(dm.getCmhcB7() + "%");
		this.bottomPanelAdditionalFieldB.setText(dm.getCmhcB8());
		this.bottomPanelAdditionalFieldC.setText(dm.getCmhcB9());
		this.bottomPanelAdditionalFieldD.setText(dm.getCmhcB10());
	}


	@Override
	public void focusGained(FocusEvent e) {
	
		
	}


	@Override
	public void focusLost(FocusEvent e) {
		dm.setCmhc1(FieldParser.parseDecimal(this.topPanelRowAField.getText()));
		dm.setCmhc2(FieldParser.parseDecimal(this.topPanelRowBField.getText()));
		dm.setCmhc3(FieldParser.parseDecimal(this.topPanelRowCField.getText()));
		dm.setCmhc4(FieldParser.parseDecimal(this.topPanelRowDField.getText()));
		dm.setCmhc5(FieldParser.parseDecimal(this.topPanelRowEField.getText()));
		dm.setCmhc6(FieldParser.parseDecimal(this.topPanelRowFField.getText()));
		dm.setCmhc7(FieldParser.parseDecimal(this.topPanelRowGField.getText()));
		dm.setCmhc8(FieldParser.parseDecimal(this.topPanelAdditionalField.getText()));
		
		dm.setCmhcB1(FieldParser.parseDecimal(this.bottomPanelRowAField.getText()));
		dm.setCmhcB2(FieldParser.parseDecimal(this.bottomPanelRowBField.getText()));
		dm.setCmhcB3(FieldParser.parseDecimal(this.bottomPanelRowCField.getText()));
		dm.setCmhcB4(FieldParser.parseDecimal(this.bottomPanelRowDField.getText()));
		dm.setCmhcB5(FieldParser.parseDecimal(this.bottomPanelRowEField.getText()));
		dm.setCmhcB6(FieldParser.parseDecimal(this.bottomPanelRowFField.getText()));
		dm.setCmhcB7(FieldParser.parseDecimal(this.bottomPanelAdditionalFieldA.getText()));
		dm.setCmhcB8(FieldParser.parseInteger(this.bottomPanelAdditionalFieldB.getText()));
		dm.setCmhcB9(FieldParser.parseInteger(this.bottomPanelAdditionalFieldC.getText()));
		dm.setCmhcB10(FieldParser.parseInteger(this.bottomPanelAdditionalFieldD.getText()));
		
		dm.updateInterface();
	}
}
